package com.example.common_base.ac

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType

/**
 * 描述：
 * 作者: zdl
 * 时间: 2020/10/29 17:01
 * 适用于kotlin与Viewbind写法的Basefragment
 */
abstract class BaseFrgKtBind<VB : ViewBinding> : Fragment() {

    private var _binding: VB? = null
    val binding: VB get() = _binding!!
    private var tagfragment = javaClass.simpleName
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        val type = javaClass.genericSuperclass as ParameterizedType
        val aClass = type.actualTypeArguments[0] as Class<*>
        val method = aClass.getDeclaredMethod("inflate",
            LayoutInflater::class.java,
            ViewGroup::class.java,
            Boolean::class.java)
        _binding = method.invoke(null, layoutInflater, container, false) as VB
        return _binding!!.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViewkt(savedInstanceState)
    }


    abstract fun initViewkt(savedInstanceState: Bundle?)


    override fun onDestroyView() {
        _binding = null
        super.onDestroyView()
    }
}